package com.qf.pkg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.core.dto.PageDto;
import com.qf.core.vo.PageResult;
import com.qf.pkg.entity.PackageOrder;
import com.qf.pkg.mapper.PackageOrderMapper;
import com.qf.pkg.service.PackageOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.product.entity.Tenant;
import com.qf.product.mapper.TenantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhu
 * @since 2023-10-27
 */
@Service
public class PackageOrderServiceImpl extends ServiceImpl<PackageOrderMapper, PackageOrder> implements PackageOrderService {
    @Autowired
    private TenantMapper tenantMapper;
    @Override
    public PageResult<PackageOrder> selectPage(PageDto<PackageOrder> pageDto) {
        IPage<PackageOrder> page = new Page<>(pageDto.getNow(),pageDto.getSize());
        //分页条件
        QueryWrapper<PackageOrder> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(pageDto.getWhere().getPayno())) {
            queryWrapper.lambda().eq(PackageOrder::getPayno, pageDto.getWhere().getPayno());
        }
        //调用查询方法
        IPage<PackageOrder> pageInfo = this.baseMapper.selectPage(page, queryWrapper);

        //去查询下单人
        for (PackageOrder record : pageInfo.getRecords()) {
            Tenant tenant = tenantMapper.selectById(record.getTenantId());
            if (tenant != null) {
                record.setTenantName(tenant.getName());
            }
        }
        PageResult pageResult = new PageResult();
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setPages(pageInfo.getPages());
        pageResult.setList(pageInfo.getRecords());

        return pageResult;
    }
}
